package com.startasone.controller;

import com.startasone.pojo.Emp;
import com.startasone.pojo.PageBean;
import com.startasone.pojo.Result;
import com.startasone.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@Slf4j
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;


    @GetMapping("/emps")
    public Result getByPage(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            String name,
                            Short gender,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        PageBean pageBean = empService.getByPage(page, pageSize,name,gender,begin,end);
        log.info("将会以这个条件查询：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }
    @DeleteMapping("/emps/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("要删除的员工id号为{}",ids);
        empService.delete(ids);
        return Result.success();
    }
    @PostMapping("/emps")
    public Result insert(@RequestBody Emp emp){
        empService.insert(emp);
        return Result.success();
    }
    @GetMapping("/emps/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("要查询的员工是{}",id);
        Emp emp=empService.getById(id);
        return Result.success(emp);
    }
    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp){
        log.info("要修改的员工id为{}",emp.getId());
        empService.update(emp);
        return Result.success();
    }
}
